package lib;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import pojo.Log;
import pojo.Log;
import pojo.Website;
import dao.LogDAO;
import dao.WebsiteDAO;

public class LogTableModel extends AbstractTableModel {
	public static final int LOGID_INDEX = 0;
	public static final int TIME_INDEX= 1;
	public static final int LOG_INDEX = 2;
	protected String[] columnNames={"LogID","Time","Content"};
	protected Vector<Log> dataVector;
	protected List<Log> delWeb=new ArrayList<Log>();
	public LogTableModel(){
		dataVector = new Vector<Log>();
		LogDAO logdao = new LogDAO();
		List<Log> logs = logdao.getList();
		dataVector.addAll(logs);
	}
	public String getColumnName(int column){
		return columnNames[column];
	}
	public Class getColumnClass(int column){
		switch(column){
		case LOGID_INDEX:
			return Integer.class;
		case LOG_INDEX:
			return String.class;
		case TIME_INDEX:		
			return Date.class;
		default:
			return Object.class;
		}
	}
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return dataVector.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		// TODO Auto-generated method stub
		Log log = dataVector.get(row);
		switch(column){
		case LOGID_INDEX:
			return log.getLogId();
		case LOG_INDEX:
			return log.getLog();
		case TIME_INDEX:
			return log.getTime();
		default:
			return log;	
		}
	}
	public void setValueAt(Object value, int row, int column){
		Log log = dataVector.get(row);
		switch(column){
		case LOGID_INDEX:
			log.setLogId((Integer)value);
			break;
		case LOG_INDEX:
			log.setLog((String)value);
			break;
		case TIME_INDEX:
			log.setTime((Date) value);
			break;
	
		default:
			break;
		}
		fireTableCellUpdated(row, column);
	}
	
	public boolean isCellEditable(int row, int column){
		return true;
	}
	public boolean hasEmptyRow(){
		if(dataVector.size() == 0)
			return true;
		return false;
	}
	public void addEmptyRow(){
		dataVector.add(new Log());
	}
	
	//public void removeRow(int n){
	//	Log log= dataVector.get(n);
		
	//	dataVector.remove(n);
	//	LogDAO dao= new LogDAO();
	//	dao.delete(log);
	//	fireTableDataChanged();
	//}
	public void removeRows(int[] rows){
		LogDAO logdao = new LogDAO();
		for(int i : rows){
			Log log = dataVector.get(i);
			//Log log = dataVector.remove(i);
			logdao.delete(log);	
		}
		dataVector.clear();
		dataVector.addAll(logdao.getList());
		fireTableDataChanged();
	}
	public void removeAll() {
		//delWeb.add(web);
		dataVector.clear();
		LogDAO dao= new LogDAO();
		dao.deleteAll();
		fireTableDataChanged();
	}
	public void updateData()
	{
		LogDAO dao = new LogDAO();
		List<Log> logs = dao.getList();
		dataVector.clear();
		dataVector.addAll(logs);
		fireTableDataChanged();
	}
	
	
}
